layui.config({
    base: '../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table
    ,admin = layui.admin;
    
    var typeCode = layui.router().search.typeCode;

     //表格管理
    table.render({
      elem: '#LAY-user-back-dict'
      ,url: "/api/admin/dictlist/page" //模拟接口
      ,cols: [[
        {type: 'numbers',title: '序号', width: 80,fixed: 'left'}
        ,{field: 'name', title: '名称'}
        ,{field: 'value', title: '值'}
        ,{field: 'label', title: '标签'}
        ,{field: 'sort', title: '排序号'}
        ,{field: 'remarks', title: '详细描述'}
        ,{title: '操作', width: 210, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
      ]]
      ,page: true
      ,where:{
        typeCode:typeCode
      }
    });

   //监听工具条
   table.on('tool(LAY-user-back-dict)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      active.del(data.id);
    } else if(obj.event === 'edit'){
      active.edit(data.id);
    } else if(obj.event === 'add'){
      active.add();
    } 
  });

  $('#add').click(function () {
    active.add();
  });

  //事件
  var active = {
    del: function(id){   
      layer.confirm('确定删除吗？', function(index) {
          delData(id, index);
      });
    },
    add: function(){
      updateOrAdd("添加字典", "");
    },
    edit:function(id){
      updateOrAdd("编辑字典", id);
    }
  }  

  
  function updateOrAdd(title, id){
    layer.open({
      type: 2
      ,title: title
      ,content: 'dictListForm.html#/id='+id+"/typeCode="+typeCode
      ,area: ['500px', '480px']
      ,btn: ['确定', '取消']
      ,yes: function(index, layero){
        var iframeWindow = window['layui-layer-iframe'+ index]
        ,submit = layero.find('iframe').contents().find("#LAY-dict-submit");

        //监听提交
        iframeWindow.layui.form.on('submit(LAY-dict-submit)', function(data){
          var field = data.field; //获取提交的字段
          //提交 Ajax 成功后，静态更新表格中的数据
         if(field.id != undefined && field.id != ""){
            subUpdateOrSave(field,"PUT", index);
         } else{
            delete field.id;
            subUpdateOrSave(field,"POST", index);
         }
         
        });  
        
        submit.trigger('click');
      }
      ,success: function(layero, index){
      
      }
    })
  }



  //修改或者添加
  function subUpdateOrSave(data, method, index){
    layer.close(index); //关闭弹层
    data._method =method;
    admin.req({
      type: method,
      url:"/api/admin/dictlist",  
      data: JSON.stringify(data),   
      dataType:"json",
      contentType:'application/json;charset=utf8',
      success:function(res){
          // 表示是修改数据
          var data = res.data;
          if(res.code==200){
            table.reload('LAY-user-back-dict'); //数据刷新
          }
      },
      error:function(jqXHR){
          alert("发生错误："+ jqXHR.status);
      }
    });
  }

  function delData(id, index){
    layer.close(index); //关闭弹层
    admin.req({
      type: "DELETE",
      url:"/api/admin/dictlist/"+id,  
      dataType:"json",
      contentType:'application/json;charset=utf8',
      success:function(res){
          var data = res.data;
          if(res.code==200){
            table.reload('LAY-user-back-dict'); //数据刷新
          }
      },
      error:function(jqXHR){
          alert("发生错误："+ jqXHR.status);
      }
    });
  }
  
  });